<template>
  <div class="dialog">
    <el-form :inline="true" :model="params" class="demo-form-inline">
      <el-form-item label="门店名称">
        <el-input placeholder="请输门店名称" v-model="params.shopName"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="params.flag">
          <el-option label="全部" value=""></el-option>
          <el-option label="启用" value="1"></el-option>
          <el-option label="禁用" value="0"></el-option></el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch" style="margin-left: 100px">查询</el-button>
        <el-button type="primary" @click="onAddShop" style="margin-left: 10px">添加门店</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tableData" border stripe style="width: 100%" v-loading="loading" size="small">
      <el-table-column prop="id" label="序号" align="center"> </el-table-column>
      <el-table-column prop="shopName" label="门店名称" align="center" :show-overflow-tooltip='true'></el-table-column>
      <el-table-column prop="shopAddress" label="门店地址" align="center" :show-overflow-tooltip='true'></el-table-column>
      <el-table-column prop="contacts" label="联系人" align="center" :show-overflow-tooltip='true'></el-table-column>
      <el-table-column prop="mobile" label="联系电话" align="center" :show-overflow-tooltip='true'></el-table-column>
      <el-table-column prop="bankName" label="打款信息" align="center" :show-overflow-tooltip='true'>
        <template slot-scope="scope">
          {{ scope.row.bankName }}  {{ scope.row.bankTitle }}  {{ scope.row.bankAccount }}  
        </template>
      </el-table-column>
      <el-table-column prop="flag" label="门店状态" align="center" :show-overflow-tooltip='true'>
        <template slot-scope="scope">
          <span v-if="scope.row.flag == 1" style="color: #67C23A;">启用</span>
          <span v-if="scope.row.flag == 0" style="color: #F56C6C;">禁用</span>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" align="center" :show-overflow-tooltip='true'></el-table-column>
      <el-table-column label="操作" align="center" fixed="right" width="130px">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="onEditShop(scope.row)">编辑</el-button>
          <el-button type="text" v-if="scope.row.flag === 1" size="mini" style="color: #F56C6C;"
            @click="onModifyStatus(scope.row, 0)">禁用</el-button>
          <el-button type="text" v-if="scope.row.flag === 0" size="mini"
            @click="onModifyStatus(scope.row, 1)">启动</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block" style="float: left; margin-top: 20px">
      <div>
        <span style="
            padding-top: 10px;
            display: inline-block;
            vertical-align: middle;
            color: #606266;
            font-size: 14px;
          ">显示第{{ this.params.page }}页的{{ this.params.rows }}条记录，总共{{
            this.total
          }}条记录
        </span>
      </div>
    </div>
    <div class="block" style="float: right; margin-top: 20px">
      <el-pagination :current-page.sync="params.page" :page-sizes="[10, 20, 50, 100]" :page-size="params.rows" background
        layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
        @current-change="handleCurrentChange" />
    </div>

    <el-dialog :visible.sync="editShopVisible" title="添加门店" width="800px" center>
      <el-form :model="shopData" ref="from" label-width="150px" :rules="rules">
        <el-form-item label="门店名称:" prop="shopName">
          <el-input v-model="shopData.shopName"></el-input>
        </el-form-item>
        <el-form-item label="门店地址:" prop="shopAddress">
          <el-input v-model="shopData.shopAddress"></el-input>
        </el-form-item>
        <el-form-item label="联系人:" prop="contacts">
          <el-input v-model="shopData.contacts"></el-input>
        </el-form-item>
        <el-form-item label="联系电话:" prop="mobile">
          <el-input v-model="shopData.mobile"></el-input>
        </el-form-item>
        <el-form-item label="打款银行:" prop="mobile">
          <el-input v-model="shopData.bankName"></el-input>
        </el-form-item>
        <el-form-item label="银行名称:" prop="mobile">
          <el-input v-model="shopData.bankTitle"></el-input>
        </el-form-item>
        <el-form-item label="银行账号:" prop="mobile">
          <el-input v-model="shopData.bankAccount"></el-input>
        </el-form-item>
        <el-form-item label="门店状态:" v-if="shopData.id == 0">
          <el-select v-model="shopData.flag">
            <el-option label="启动" :value="1"></el-option>
            <el-option label="禁用" :value="0"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editShopVisible = false">关 闭</el-button>
        <el-button type="primary" @click="handleEditShop">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog :visible.sync="modifyVisible" title="提示" width="450px" center>
      <div class="del-dialog-cnt">确定修改门店状态？</div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="modifyVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleModifyStatus">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>
<script>
import {
  getShopList, editShop, updateShopStatus
} from "@/api/carSource";
import _ from 'lodash';

export default {
  data() {
    return {
      loading: false,
      currentPage4: 4,
      total: 0,
      params: {
        shopName: '',
        flag:''
      },
      tableData: [],
      editShopVisible: false,
      modifyVisible: false,
      shopData: {
        id: 0,
        shopName: '',
        shopAddress: '',
        contacts: '',
        mobile: '',
        flag: 1,
        bankTitle:'',
        bankName:'',
        bankAccount:''
      },
      rules: {
        shopName: [
          { required: true, message: '请输入门店名称', trigger: 'blur' },
        ],
        shopAddress: [
          { required: true, message: '请输入门店地址', trigger: 'blur' }
        ],
        contacts: [
          { required: true, message: '联系人', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '联系人电话', trigger: 'blur' }
        ]
      }
    };
  },

  methods: {
    onSearch() {
      this.getData();
    },
    handleSizeChange(val) {
      this.params.rows = val;
      this.getData();
    },
    handleCurrentChange(val) {
      this.params.page = val;
      this.getData();
    },
    getData(exportExcel) {
      if (exportExcel) {
        this.params.export = true
        // parkIncomeexport(this.params, "车场收入明细")
      } else {
        this.loading = true;
        this.params.export = false
        let params = {
          pageSize: this.params.rows,
          pageNo: this.params.page,
          filter: [{
            field: 'shopName',
            operator: 'LIKE',
            value: this.params.shopName
          }],
          orderType:'desc',
          orderField:'id'
        }
        if(this.params.flag!=''){
          params.filter.push({
            field: 'flag',
            operator: 'EQ',
            value: this.params.flag
          })
        }
        getShopList(params).then((res) => {
          this.loading = false;
          this.total = res.data.total;
          this.tableData = res.data.rows;
        });
      }
    },
    onExport() {
      this.getData(true)
    },
    onEditShop(row) {
      this.resetForm();
      this.editShopVisible = true;
      this.shopData = _.clone(row);
    },
    handleEditShop() {
      this.$refs['from'].validate((valid) => {
        if (valid) {
          editShop(this.shopData).then((res) => {
            this.getData();
            this.editShopVisible = false;
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    onModifyStatus(row, flag) {
      this.modifyVisible = true;
      this.shopData.id = row.id;
      this.shopData.flag = flag;
    },
    handleModifyStatus() {
      updateShopStatus(this.shopData.id, this.shopData.flag).then((res) => {
        this.getData();
        this.modifyVisible = false;
      });
    },
    onAddShop() {
      this.resetForm();
      this.editShopVisible = true;
    },
    resetForm() {
      this.shopData = {
        id: 0,
        shopName: '',
        shopAddress: '',
        contacts: '',
        mobile: '',
        flag: 1,
        bankTitle:'',
        bankName:'',
        bankAccount:''
      }
    }
  },
  mounted() {
    this.getData();
  },
};
</script>
<style scoped lang="scss">
.my-radio {
  margin-bottom: 10px;
}

.dialog {
  width: 95%;
  height: 100%;
  padding-top: 10px;
  margin-left: 10px;
}
</style>
